package com.apache.bid.service.impl;

import java.util.List;

import com.apache.api.vo.ParamsVo;
import com.apache.bid.common.Constant;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.entity.BidItemVip;
import com.apache.bid.manager.BidItemVipManager;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.tools.DateUtils;

public class BidItemVipManagerImpl implements BidItemVipManager {

	protected IDao bidItemVipDao;
	protected final String entityName = "com.apache.bid.entity.BidItemVip";

	public void setBidItemVipDao(IDao bidItemVipDao) {
		this.bidItemVipDao = bidItemVipDao;
	}

	public String saveInfo(ParamsVo<BidItemVip> vo) throws BusinessException {
		BidItemVip bidItemVip = vo.getObj();
		String infoId = Validator.generate();
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="bidItemVip_"+infoId;
		if (Validator.isNull(bidItemVip.getVipId())) {
			bidItemVip.setVipId(infoId);
			bidItemVip.setCreateIp(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
		}
		if (Constant.getInstance().ifWriteDatabase()) {
			if (Validator.isNull(bidItemVip.getVipLevel())) {
				bidItemVip.setVipLevel("0");
			}
			if (Validator.isNull(bidItemVip.getVipStatus())) {
				bidItemVip.setVipStatus("1");
			}
			bidItemVip.setCreateTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			MethodParam param = new MethodParam("BidItemVip", cacheKey, "", entityName);
			param.setVaule(bidItemVip);
			if (bidItemVipDao.insert(param)) {
				BidCacheFactory.getInstance().getCacheManager("itemVip").createCacheObject(bidItemVip);
				return bidItemVip.getVipId();
			}
		} else {
			///////////////需要处理缓存结果//////////////////
			vo.setParams("methodCode", "saveItemVip");
			return bidItemVip.getVipId();
		}
		return "";
	}

	public boolean editInfo(ParamsVo<BidItemVip> vo) throws BusinessException {
		BidItemVip bidItemVip = vo.getObj();
		if (Validator.isNotNull(bidItemVip.getVipId())) {
			bidItemVip.setModidyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
			String cacheKey = "";
			//String cacheKey="bidItemVip_"+bidItemVip.getVipId();
			if (Constant.getInstance().ifWriteDatabase()) {
				bidItemVip.setModidyTime(DateUtils.Now.fmt_yyyyMMdd_HHmmss());
				MethodParam param = new MethodParam("BidItemVip", cacheKey, "", entityName);
				param.setVaule(bidItemVip);
				boolean mark = bidItemVipDao.edit(param);
				if (mark) {
					param.setKey("ById");
					param.setInfoId(bidItemVip.getVipId());
					bidItemVip = (BidItemVip) bidItemVipDao.selectById(param);
					BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("itemVip");
					if (cache.checkCacheObject(cache.item_vip_prefix + bidItemVip.getUserEname() + "_"
							+ bidItemVip.getItemId())) {
						cache.createCacheObject(bidItemVip);
					}
				}
				return mark;
			} else {
				///////////////需要处理缓存结果//////////////////
				vo.setParams("methodCode", "editItemVip");
				return true;
			}
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<BidItemVip> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="bidItemVip_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		if (Constant.getInstance().ifWriteDatabase()) {
			MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
			param.setInfoId(infoId);
			BidItemVip info = (BidItemVip) bidItemVipDao.selectById(param);
			if (Validator.isEmpty(info)) {
				return false;
			}
			boolean flag = false;
			if ("false".equals(mark)) {//逻辑删除
				param.setVaule(info);//此处需要先将状态值赋值为删除状态
				param.setKey("BidItemVip");
				flag = bidItemVipDao.edit(param);
			} else {
				param.setParams("vipId", infoId);
				param.setDelete(true);
				flag = bidItemVipDao.delete(param);
			}
			if (flag) {
				BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("itemVip");
				cache.removeCacheObject(cache.item_vip_prefix + info.getUserEname() + "_" + info.getItemId());
			}
			return flag;
		} else {
			///////////////需要处理缓存结果//////////////////
			if ("false".equals(mark)) {//逻辑删除
			} else {
			}
			vo.setParams("methodCode", "deleteItemVip");
			return true;
		}
	}

	public Object getInfoById(ParamsVo<BidItemVip> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="bidItemVip_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return bidItemVipDao.selectById(param);
	}

	public Object execute(ParamsVo<BidItemVip> vo) {
		String methodKey = vo.getMethodKey();
		if ("checkUserEname".equalsIgnoreCase(methodKey)) {
			return checkUserEname(vo.getObj());
		}
		return null;
	}

	//检查登录名是否存在
	private boolean checkUserEname(BidItemVip itemVip) {
		MethodParam param = new MethodParam("ByProperty", "", "", entityName);
		StringBuffer sb = new StringBuffer();
		if (Validator.isNotNull(itemVip.getItemId()) && !"0".equals(itemVip.getItemId())) {
			sb.append(" and itemId =:itemId");
			param.setParams("itemId", itemVip.getItemId());
		}
		if (Validator.isNotNull(itemVip.getUserEname())) {
			sb.append(" and userEname =:userEname");
			param.setParams("userEname", itemVip.getUserEname());
		}
		param.setSqlStr(bidItemVipDao.getSql(3) + sb.toString());
		long count = bidItemVipDao.count(param);
		if (count > 0)
			return true;
		return false;
	}

	public Page getPageInfo(ParamsVo<BidItemVip> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		Page page = bidItemVipDao.pageSelect(param);
		return page;
	}

	public List<BidItemVip> getList(ParamsVo<BidItemVip> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderBy = Validator
				.getDefaultStr(String.valueOf(vo.getParams("orderBy")), " order by create_time desc ");
		param.setParams("orderBy", orderBy);
		return bidItemVipDao.select(param);
	}

	public long countInfo(ParamsVo<BidItemVip> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return bidItemVipDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<BidItemVip> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(bidItemVipDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		BidItemVip bidItemVip = vo.getObj();
		param.setResultCache(false);
		if (Validator.isEmpty(bidItemVip))
			return param;
		if (Validator.isNotNull(bidItemVip.getItemId())) {
			sb.append(" and itemId = :itemId");
			param.setParams("itemId", bidItemVip.getItemId());
		}
		if (Validator.isNotNull(bidItemVip.getUserEname())) {
			sb.append(" and userEname = :userEname");
			param.setParams("userEname", bidItemVip.getUserEname());
		}
		if (Validator.isNotNull(bidItemVip.getUserCname())) {
			sb.append(" and userCname = :userCname");
			param.setParams("userCname", bidItemVip.getUserCname());
		}
		if (Validator.isNotNull(bidItemVip.getVipType())) {
			sb.append(" and vipType = :vipType");
			param.setParams("vipType", bidItemVip.getVipType());
		}
		if (Validator.isNotNull(bidItemVip.getVipLevel())) {
			sb.append(" and vipLevel = :vipLevel");
			param.setParams("vipLevel", bidItemVip.getVipLevel());
		}
		if (Validator.isNotNull(bidItemVip.getVipStatus())) {
			sb.append(" and vipStatus = :vipStatus");
			param.setParams("vipStatus", bidItemVip.getVipStatus());
		}
		if (Validator.isNotNull(bidItemVip.getCreateUserEname())) {
			sb.append(" and createUserEname = :createUserEname");
			param.setParams("createUserEname", bidItemVip.getCreateUserEname());
		}
		if (Validator.isNotNull(bidItemVip.getCreateIp())) {
			sb.append(" and createIp = :createIp");
			param.setParams("createIp", bidItemVip.getCreateIp());
		}
		if (Validator.isNotNull(bidItemVip.getCreateTime())) {
			sb.append(" and createTime = :createTime");
			param.setParams("createTime", bidItemVip.getCreateTime());
		}
		if (Validator.isNotNull(bidItemVip.getModidyTime())) {
			sb.append(" and modidyTime = :modidyTime");
			param.setParams("modidyTime", bidItemVip.getModidyTime());
		}
		if (Validator.isNotNull(bidItemVip.getSpare1())) {
			sb.append(" and spare1 = :spare1");
			param.setParams("spare1", bidItemVip.getSpare1());
		}
		if (Validator.isNotNull(bidItemVip.getSpare2())) {
			sb.append(" and spare2 = :spare2");
			param.setParams("spare2", bidItemVip.getSpare2());
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
